
#include "sdk_common.h"
#include "sdk_model_CAC.h"
#include "sdk_proto.h"
#include <stdio.h>
#include <string.h>
#include "sdk.h"




int sdk_model_CAC_ctl(ENUM_CAC_CTL_TYPE type,uint16_t addr,uint16_t subaddr, P_CAC_DATA data)
{
    char json[500]={0};
    char ctldata[100]={0};
    uint16_t attrid= 0;
    if(data== NULL){
        sdk_printf("%s:null pointer\r\n",__FUNCTION__);
        return SDK_NULLPTR_ERR;
    }
    if(addr==0){
        sdk_printf("%s:addr==0\r\n",__FUNCTION__);
        return SDK_PARAMS_ERR;
    }
    if(subaddr==0){
        sdk_printf("%s:subaddr==0\r\n",__FUNCTION__);
        return SDK_PARAMS_ERR;
    }
    if(sdk_proto_context_get()==NULL){
        sdk_printf("%s:sdk context is null\r\n",__FUNCTION__);
        return SDK_UNAUTH_ERR;
    }

    memset(ctldata,0,sizeof(ctldata));
    attrid = SDK_CAC_CTL_ATTRID;
    subaddr = ((subaddr>>8)&0xFF) |  ((subaddr&0xFF)<<8);
    switch(type)
    {
        //D1F9092B4D1066010100
        case enum_CAC_ctl_onoff:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_onoff,data->onoff);
        break;
        //D1F909334D106601021D
        case enum_CAC_ctl_temp:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_temp,data->temp);
        break;
        //D1F9092E4D1066010301
        //D1F9092F4D1066010308
        //D1F909304D1066010304
        //D1F909314D1066010302
        case enum_CAC_ctl_mode:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_mode,data->mode);
        break;
        //D1F9092D4D1066010402
        case enum_CAC_ctl_windspeed:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_windspeed,data->windspeed);
        break;
        case enum_CAC_ctl_antifreeze:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_antifreeze,data->antiFreeze);
        break;
        case enum_CAC_ctl_huminity:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_huminity,data->huminity);
        break;
        case enum_CAC_ctl_clearness:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_clearness,data->clearness);
        break;      
        case enum_CAC_ctl_winddirection:
            sprintf(ctldata,"%04x%02x%02x",subaddr,enum_CAC_ctl_winddirection,data->winddirection);
        break;   
        default:
            sdk_printf("%s:unkown option\r\n",__FUNCTION__);
        break;
    }


    sprintf(json,"{\"call\":\"ai.argrace.gw.data.set\",\"ak\":\"%s\",\"gwid\":%u,\"ad\":\"%04x\",\"op\":\"%s\",\"vma\":\"%02x%02x\",\"data\":\"%s\",\"tid\":\"%02x\",\"mac\":\"%s\"}",\
        sdk_proto_context_get()->accesskey,sdk_proto_context_get()->gwid,addr,VMAD1,attrid&0xFF,(attrid>>8)&0xFF,ctldata,sdk_proto_get_tid(),sdk_context.mac);

    sdk_printf("%s:%s\r\n",__FUNCTION__,json);
    return sdk_proto_ctl_send((uint8_t *)json,strlen(json)+1);
}


















